﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Universe.HpglParser
{
    using System.IO;
    using VectorImage;

    class SharpWriter
    {
        public static void Write(TextWriter writer, IEnumerable<SimpleLine> lines, string fullTypeName)
        {
            if (writer == null)
                throw new ArgumentNullException("writer");

            if (lines == null)
                throw new ArgumentNullException("lines");

            if (fullTypeName == null)
                throw new ArgumentNullException("fullTypeName");

            string[] arr = fullTypeName.Split('.');
            if (arr.Length < 2)
                throw new ArgumentException("fullTypeName argument should containt namespace and class type");

            string ns = string.Join(".", arr.Take(arr.Length - 1));
            string c = arr.Last();
            string code_prev = string.Join(
                Environment.NewLine,
                lines.Select(x =>
                             new string(' ', 10)
                             + string.Format(
                                 "new Universe.VectorImage.SimpleLine({0}, {1}, {2}, {3}, {4}),",
                                 x.Pen, x.X1, x.Y1, x.X2, x.Y2)
                    ));

            string code = string.Join(
                Environment.NewLine,
                lines.Select(x =>
                             new string(' ', 12)
                             + string.Format(
                                 "{0}, {1}, {2}, {3}, {4},",
                                 x.Pen, x.X1, x.Y1, x.X2, x.Y2)
                    ));
            
            

            writer.WriteLine(SharpCode_Prev, ns, c, code);
        }

        private const string SharpCode_Prev = @"
namespace {0}
{{
    partial class {1}
    {{
        public static readonly Universe.VectorImage.SimpleImage Image = new Universe.VectorImage.SimpleImage(new[] {{
{2}
        }});
    }}
}}
";

    }

}
